import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
import { Observable } from 'rxjs'
import { Reflector } from '@nestjs/core'
// @ts-ignore
import { user } from '@prisma/client'
import { Role } from '../unum/role'
@Injectable()
export class AuthGuardGuard implements CanActivate {
  constructor(private reflector: Reflector) {}
  canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
    const user = context.switchToHttp().getRequest().user as user

    const roles = this.reflector.getAllAndMerge<Role[]>('role', [context.getHandler(), context.getClass()])
    return roles.length > 0 ? roles.includes(user.role as Role) : true
  }
}
